<?php

class Tourismprovidercategory extends CS_Controller
{
    public function _init()
    {
        $this->load->library('pagination');
        $this->load->model('tourism_provider_category_model', 'tourism_provider_category');
        $this->load->model('tourism_goods_base_model', 'tourism_goods_base');
    }

    /**
     * 首页
     * @param number $pg
     */
    public function grid($pg = 1)
    {
        $page_num = 20;
        $num = ($pg - 1) * $page_num;
        $config['first_url'] = base_url('tourismprovidercategory/grid') . $this->pageGetParam($this->input->get());
        $config['suffix'] = $this->pageGetParam($this->input->get());
        $config['base_url'] = base_url('tourismprovidercategory/grid');
        $config['total_rows'] = $this->tourism_provider_category->total($this->input->get());
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_list'] = $this->pagination->create_links();
        $data['page_list'] = $this->tourism_provider_category->page_list($page_num, $num, $this->input->get());
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['url'] = $this->pageGetParam($this->input->get());
        $this->load->view('tourismprovidercategory/grid', $data);
    }


    /**
     * 上下架
     * @param unknown $id
     * @param unknown $flag
     */
    public function toggle()
    {
        $id = $this->input->post('id');
        $status = $this->input->post('flag');
        switch ($status) {
            case '1':
                $flag = '2';
                break;
            case '2':
                $flag = '1';
                break;
            default :
                $flag = '2';
                break;
        }
        $this->db->trans_start();
        $isUpdate = $this->tourism_provider_category->updateTourismProviderCategoryFlag($flag, $id);
        $this->db->trans_complete();

        if ( $this->db->trans_status() === TRUE && $isUpdate ) {
            echo json_encode(array(
                'flag' => $flag,
            ));
        } else {
            echo json_encode(array(
                'flag' => 3,
            ));
        }
        exit;
    }
    
     /**
     * 批量上下架
     */
    public function batchIsOnSale() {
    	
        $flag = (int)$this->input->post('type');
        $pg = (int)$this->input->post('pg');
        $idArray = $this->input->post('id');
        if (count($idArray) <=0) {
            $this->jsonMessage('请选择类别');
        }
        if (empty($flag)) {
            $this->jsonMessage('请选择操作类型');
        }
        $result = $this->tourism_provider_category->updateTourismProviderCategoryFlag($flag, $idArray);
        if($result) {
            $this->session->set_flashdata('success', '操作成功!');
            $this->jsonMessage('',site_url('tourismprovidercategory/grid/'.$pg));
        }
        $this->session->set_flashdata('error', '操作失败!');
        $this->jsonMessage('操作失败');
    }

    public function delete($id, $isMain)
    {

        if ( $isMain == 2 ) {
            $this->error('tourismprovidercategory/grid', '', '该类别为商家供应商主推类，不能删除');
        }
        $result = $this->tourism_goods_base->findResultByProviderCategoryId($id);
        if ( $result->num_rows() > 0 ) {
            $this->error('tourismprovidercategory/grid', '', '该类别已被商家供应商使用');
        }
        $status = $this->tourism_provider_category->delete($id);
        if ( $status ) {
            $this->success('tourismprovidercategory/grid', '', '删除成功');
        } else {
            $this->error('tourismprovidercategory/grid', '', '删除失败');
        }
    }


    /**
     * @descripte   添加页面
     * @Author xiumao
     * @date 2016/6/8 0008 上午 9:08
     */
    public function add()
    {
        $data['uid'] = $this->input->get('uid',true);
        $this->load->view('tourismprovidercategory/add', $data);
    }

    /**
     * @descripte   添加页面 insert
     * @Author xiumao
     * @date 2016/6/8 0008 上午 9:29
     */
    public function addPost()
    {
        $params = $this->input->post();
        /* step1 验证不为空*/
        $error = $this->validate($params);
        /* step2 验证同一供应商 类别是否重复*/
        $result = $this->tourism_provider_category->checkUidCategoryName($params['provider_uid'], $params['category_name'], $id = false);
        if ( $result->num_rows() > 0 ) {
            $error['category_name'] = '类别名称重复';
        }
        if ( !empty($error) ) {
            $this->error('tourismprovidercategory/grid', '', $error);
        }
        $this->db->trans_start();
        $this->tourism_provider_category->insertProviderCategory($params);
        /* step3 只有一个主推*/
        if ( $params['is_main'] == 2 ) {
            $id = $this->db->insert_id();
            $this->tourism_provider_category->setProviderCategoryMain($params, $id);
        }
        $this->db->trans_complete();
        if ( $this->db->trans_status === false ) {
            $this->error('tourismprovidercategory/add', array('uid'=>$params['provider_uid']), '添加失败！');
        } else {
            $this->success('tourismprovidercategory/grid', array('uid'=>$params['provider_uid']), '添加成功');
        }
    }

    public function checkUidCategoryName()
    {
        $params = $this->input->post();
        $params['id'] = isset($params['id']) ? $params['id'] : false;
        $result = $this->tourism_provider_category->checkUidCategoryName($params['provider_uid'], $params['category_name'], $params['id']);

        echo $result->num_rows() > 0 ? 'false' : 'true';
        exit;
    }

    /**
     * @descripte   修改页面
     * @Author xiumao
     * @date 2016/6/8 0008 上午 11:05
     */
    public function edit($id)
    {
        $result = $this->tourism_provider_category->findById($id);
        if ( $result->num_rows() <= 0 ) {
            $this->error('tourismprovidercategory/grid', '', '没有该序列值');
        }
        $data['providercategory'] = $result->row(0);
        $this->load->view('tourismprovidercategory/edit', $data);
    }

    /**
     * @descripte   修改页面    update
     * @Author xiumao
     * @date 2016/6/8 0008 上午 11:18
     */
    public function editPost()
    {
        $params = $this->input->post();
        $id = $params['id'];
        /* step1 验证不为空*/
        $error = $this->validate($params);
        /* step2 验证同一供应商 类别是否重复*/
        $result = $this->tourism_provider_category->checkUidCategoryName($params['provider_uid'], $params['category_name'], $id);
        if ( $result->num_rows() > 0 ) {
            $error['category_name'] = '类别名称重复';
        }
        if ( !empty($error) ) {
            $this->error('tourismprovidercategory/grid', '', $error);
        }
        $this->db->trans_start();
        $this->tourism_provider_category->updateProviderCategory($params);
        /* step3 只有一个主推*/
        if ( $params['is_main'] == 2 ) {
            $this->tourism_provider_category->setProviderCategoryMain($params, $id);
        }
        $this->db->trans_complete();
        if ( $this->db->trans_status === false ) {
            $this->error('tourismprovidercategory/edit', $id, '修改失败！');
        } else {
            $this->success('tourismprovidercategory/grid', '', '修改成功');
        }
    }

    /**
     * @descripte   验证
     * @Author xiumao
     * @date 2016/6/8 0008 上午 9:40
     */
    public function validate($postData)
    {
        $error = array();
        if ( $this->validateParam($postData['provider_uid']) ) {
            $error['provider_uid'] = '供应商uid不能为空';
        }
        if ( $this->validateParam($postData['category_name']) ) {
            $error['category_name'] = '类别名称不能为空';
        }
        if ( $this->validateParam($postData['is_main']) ) {
            $error['is_main'] = '是否主推不能为空';
        }
        if ( $this->validateParam($postData['is_on_sale']) ) {
            $error['is_on_sale'] = '上下架不能为空';
        }
        if ( $this->validateParam($postData['sort']) ) {
            $error['sort'] = '排序不能为空';
        }

        return $error;
    }


}